package com.mc.jf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mc.jf.entity.PlanUser;
import com.mc.jf.mapper.PlanMapper;
import com.mc.jf.mapper.PlanUserMapper;
import com.mc.jf.service.IPlanUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mc.jf.utils.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 计划小程序_用户 服务实现类
 * </p>
 *
 * @author Lin
 * @since 2023-05-03
 */
@Service
public class PlanUserServiceImpl extends ServiceImpl<PlanUserMapper, PlanUser> implements IPlanUserService , UserDetailsService {



    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper<PlanUser> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",username);
        PlanUser user= getOne(queryWrapper);
        if (user==null){
            throw new UsernameNotFoundException("不存在");
        }
        System.out.println("========PlanUserServiceImpl:"+user);
        List<String> auths=new ArrayList<>();
        if ("admin".equals(user.getUsername())){
            auths.add(AuthUtil.planEdit);
            auths.add(AuthUtil.jfEdit);
            auths.add(AuthUtil.bzIndex);
            return new PlanUser(user.getId(),user.getUsername(),user.getPwd(),user.getUnit(),PlanUser.roleAdmin,auths);
        }else {
            auths.add(AuthUtil.planEdit);
            return new PlanUser(user.getId(),user.getUsername(),user.getPwd(),user.getUnit(),PlanUser.rolePlan,auths);
        }

    }
}
